package com.mangocheese.subject.domain.service.impl;

import com.alibaba.fastjson.JSON;

import com.mangocheese.subject.common.enums.IsDeletedFlagEnum;
import com.mangocheese.subject.domain.convert.SubjectLabelConverter;
import com.mangocheese.subject.domain.entity.SubjectLabelBO;
import com.mangocheese.subject.domain.service.SubjectLabelDomainService;
import com.mangocheese.subject.infra.basic.entity.SubjectLabel;
import com.mangocheese.subject.infra.basic.entity.SubjectMapping;
import com.mangocheese.subject.infra.basic.service.SubjectLabelService;
import com.mangocheese.subject.infra.basic.service.SubjectMappingService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {

    @Resource
    private SubjectLabelService subjectLabelService;

    @Resource
    private SubjectMappingService subjectMappingService;
//
//    @Resource
//    private SubjectCategoryService subjectCategoryService;

    @Override
    public Boolean add(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainServiceImpl.add.bo:{}", JSON.toJSONString(subjectLabelBO));
        }
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE
                .convert(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        return subjectLabelService.insert(subjectLabel);
    }

    @Override
    public Boolean update(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainServiceImpl.update.bo:{}", JSON.toJSONString(subjectLabelBO));
        }
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE
                .convert(subjectLabelBO);
        return subjectLabelService.update(subjectLabel);
    }

    @Override
    public Boolean delete(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainServiceImpl.update.bo:{}", JSON.toJSONString(subjectLabelBO));
        }
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE
                .convert(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
        return subjectLabelService.update(subjectLabel);
    }

    @Override
    public List<SubjectLabelBO> queryLabel(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainServiceImpl.queryLabel.bo:{}", JSON.toJSONString(subjectLabelBO));
        }
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE
                .convert(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        List<SubjectLabel> subjectLabelList = subjectLabelService.queryLabel(subjectLabel);
        List<SubjectLabelBO> subjectLabelBOList = SubjectLabelConverter.INSTANCE
                .convert(subjectLabelList);
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainServiceImpl.queryLabel.boList:{}", JSON.toJSONString(subjectLabelBOList));
        }
        return subjectLabelBOList;
    }

    @Override
    public List<SubjectLabelBO> queryLabelByCategoryId(SubjectLabelBO subjectLabelBO) {

        //如果当前分类是1级分类，则查询所有标签
//        SubjectCategory subjectCategory = subjectCategoryService.queryById(subjectLabelBO.getCategoryId());
//        if(CategoryTypeEnum.PRIMARY.getCode() == subjectCategory.getCategoryType()){
//            SubjectLabel subjectLabel = new SubjectLabel();
//            subjectLabel.setCategoryId(subjectLabelBO.getCategoryId());
//            List<SubjectLabel> labelList = subjectLabelService.queryByCondition(subjectLabel);
//            List<SubjectLabelBO> labelResultList = SubjectLabelConverter.INSTANCE.convertLabelToBoList(labelList);
//            return labelResultList;
//        }

        SubjectMapping subjectMapping = new SubjectMapping();
        subjectMapping.setCategoryId(subjectLabelBO.getCategoryId());
        subjectMapping.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());

        List<SubjectMapping> mappingList = subjectMappingService.queryLabelId(subjectMapping);
        if (CollectionUtils.isEmpty(mappingList)) {
            return Collections.emptyList();
        }
        List<Long> labelIdList = mappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());

        // 代码去重
//        List<SubjectMapping> subjectMappings = subjectMappingService.queryMapping(subjectMapping);
//        if (CollectionUtils.isEmpty(subjectMappings)) {
//            return Collections.emptyList();
//        }
//        Set<Long> labelIdSet = subjectMappings.stream().map(SubjectMapping::getLabelId).collect(Collectors.toSet());

        List<SubjectLabel> subjectLabelList = subjectLabelService.queryByIds(labelIdList);
        List<SubjectLabelBO> subjectLabelBOList = SubjectLabelConverter.INSTANCE.convert(subjectLabelList);
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainServiceImpl.queryLabelByCategoryId.boList:{}", JSON.toJSONString(subjectLabelBOList));
        }
        return subjectLabelBOList;
    }
}
